package com.example.cinemaorder.service.impl;

import com.example.cinemaorder.entity.vo.SearchMovieVo;
import com.example.cinemaorder.entity.vo.UpdateMovieSaleVo;
import com.example.cinemaorder.mapper.SysSessionMapper;
import com.example.cinemaorder.service.SysMovieService;
import com.example.cinemaorder.service.SysSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 高垚淼
 * @version 1.0
 */
@Service
public class SysSessionServiceImpl implements SysSessionService {

    @Autowired
    SysSessionMapper sysSessionMapper;
    @Autowired
    SysMovieService sysMovieService;
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public Object findSessionById(Long id) {
        String posterByid = sysSessionMapper.findPosterByid(id);
        ArrayList movieCategoryById = sysSessionMapper.findMovieCategoryById(id);
        String category = new String();
        for(int i=0;i<movieCategoryById.size();i++){
            category = category+movieCategoryById.get(i)+" ";
        }
        String nameById = sysSessionMapper.findNameById(id);
        SearchMovieVo session = sysSessionMapper.findSessionById(id);
        session.setPoster(posterByid);
        session.setCategory(category);
        session.setName(nameById);
        return session;
    }

    @Override
    public Object findPosterByid(Long id) {
        return sysSessionMapper.findPosterByid(id);
    }

    @Override
    public Object findMovieCategoryById(Long id) {
        return sysSessionMapper.findMovieCategoryById(id);
    }

    @Override
    public Object findNameById(Long id) {
        return sysSessionMapper.findNameById(id);
    }

    @Override
    public Object getNewSeats(Long id) {
        return sysSessionMapper.getNewSeats(id);
    }

    @Override
    public void updateSeats(Long id ,int updateSallNums,String sessionSeats) {
        sysSessionMapper.updateSeats(id,updateSallNums,sessionSeats);
    }

    @Override
    public void updateSallNums(Long sessionId, int num) {
        sysSessionMapper.updateSallNums(sessionId,num);
    }

    @Override
    public void updateSession(SearchMovieVo curSession) {
        sysSessionMapper.updateSession(curSession);
    }

    @Override
    public void updateCancelState(Long billId) {
        sysSessionMapper.updateCancelState(billId);
    }

    @Override
    public void updateMovieSale(UpdateMovieSaleVo updateMovieSaleVo) {
        sysSessionMapper.updateMovieSale(updateMovieSaleVo);
    }

    @Override
    public Long getMovieId(Long billId) {
        return sysSessionMapper.getMovieId(billId);
    }

    @Override
    public void updateMovieRating() {
        List<Integer> ids = (ArrayList<Integer>) sysMovieService.getIds();
        //根据id去redis查询出数据
        for (int id = 1; id <= ids.size(); id++) {
            Map entries = redisTemplate.opsForHash().entries("电影评分：" + id);
            // 评分总人数
            Double userId = Double.parseDouble( ""+entries.keySet().toArray()[entries.keySet().toArray().length-1]);
            // 平均评分
            Double rating = Double.parseDouble(""+ entries.values().toArray()[entries.values().toArray().length-1]);
            sysSessionMapper.updateMovieRating(id,Double.parseDouble(userId.toString()),rating);
        }
    }
}
